#include "Rebot.h"
#include "Common/TableData/ArenaRobotCfg.h"
#include "Character/PackagePart.h"
#include "Character/PlayerMgr.h"
#include "Character/newAI/behaviac/CreatureCommonAI.h"
#include "Character/newAI/behaviac/BehaviacAIManager.h"
#include "Common/AttrMgr/AttrMgr.h"
CRebot::CRebot()
{
	m_startAiTime = 0;
	m_creatureKind = CREATURE_REBOT;
	m_sightRange = 25.0f;
	m_nRebotID = 0;
	m_bCanBeSeen = true;
}

CRebot::~CRebot()
{

}

void CRebot::AddSeeNewCreature(Creature* pCreature)
{
	if (pCreature && pCreature->CreatureKind() == CREATURE_PLAYER)
	{
		if (m_aiData)
		{
			m_aiData->AddHateTarget(pCreature->GetCid(), 0, 0);
		}
	}
}

bool CRebot::IsFriendSide(Creature* pCreature)
{
	if (pCreature)
	{
		uint8_t kind = pCreature->CreatureKind();
		if (kind == CREATURE_PLAYER)
		{
			return false;
		}
	}
	return true;
}

bool CRebot::Init(uint32_t rebotID, CharIDType cid)
{
	memset(m_attrs, 0, sizeof(uint32_t)*P_MAX);
	for (uint32_t i = 0; i < PART_MAX; i++)
	{
		m_pPart[i] = nullptr;
	}

	m_nRebotID = rebotID;
	m_Cid = cid;

	const ArenaRobotCfgInfo * pConfig = g_GetArenaRobotCfgTable()->GetArenaRobotCfgInfo(rebotID);
	if (!pConfig)
	{
		return false;
	}


	m_name = pConfig->robotname;
	//m_userName= pConfig->robotname;

	//填充属性
	m_attrs[P_HP] = pConfig->maxHP;
	m_attrs[P_MAX_HP] = pConfig->maxHP;

	m_attrs[P_MP] = pConfig->maxMP;
	m_attrs[P_MAX_MP] = pConfig->maxMP;
	//m_attrs[P_AP] = pConfig->ap;
	//m_attrs[P_MAT] = pConfig->mat;
	//m_attrs[P_DP] = pConfig->dp;
	//m_attrs[P_MDP] = pConfig->mdp;
	//m_attrs[P_PHYHIT] = pConfig->phyHit;
	//m_attrs[P_MAGHIT] = pConfig->magHit;
	//m_attrs[P_PHYDODGE] = pConfig->phyDodge;
	//m_attrs[P_MAGDODGE] = pConfig->magDodge;

	//m_attrs[P_PHYCRIT] = pConfig->phyCrit;
	//m_attrs[P_MAGCRIT] = pConfig->magCrit;

	////m_attrs[P_PHYCRIT] = pConfig->phyCrit;
	////m_attrs[P_MAGCRIT] = pConfig->magCrit;

	//m_attrs[P_PHYTOUGHNESS] = pConfig->phyToughness;
	//m_attrs[P_MAGTOUGHNESS] = pConfig->magToughness;

	//m_attrs[P_PHYCRITMUL] = pConfig->phyCritMul;
	//m_attrs[P_MAGCRITMUL] = pConfig->magCritMul;

	m_attrs[P_FIGHT_POWER] = pConfig->sword;
	m_attrs[P_PROF] = pConfig->robotProfession;
	m_attrs[P_GENDER] = pConfig->sex;
	//m_attrs[P_MSPEED] = m_fspeed * 1000;
	m_attrs[P_LEVEL] = pConfig->robotLv;



	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAX_HP), pConfig->maxHP);
	//SetAttrCache(g_GetAttrMgr()->GetPlayerLevPAttr(C_MAX_HP), pConfig->maxHP);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAX_MP), pConfig->maxMP);
	//SetAttrCache(g_GetAttrMgr()->GetPlayerLevPAttr(C_MAX_MP), pConfig->maxMP);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level,C_AP),pConfig->ap);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAT), pConfig->mat);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_DP), pConfig->dp);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MDP), pConfig->mdp);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_PHYHIT), pConfig->phyHit);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAGHIT), pConfig->magHit);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_PHYDODGE), pConfig->phyDodge);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAGDODGE), pConfig->magDodge);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_PHYCRIT), pConfig->phyCrit);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAGCRIT), pConfig->magCrit);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_PHYTOUGHNESS), pConfig->phyToughness);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAGTOUGHNESS), pConfig->magToughness);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_PHYCRITMUL), pConfig->phyCritMul);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MAGCRITMUL), pConfig->magCritMul);
	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MSPEED), pConfig->mSpeed);


	//填充各部件信息
	CharacterDBInfo tmpInfo;
	//技能
	CharacterDBSkillData * pSkillData = tmpInfo.mutable_skilldata();
	for (int i = 0; i < (int)pConfig->vecArenaRobotActCfg.size(); ++i)
	{
		CharacterDBSkillItem* pSkillItem = pSkillData->add_skilllist();
		pSkillItem->set_skillid(pConfig->vecArenaRobotActCfg[i].Skill);
		if ((int)pConfig->vecArenaRobotSkillCfg.size()>i)
		{
			pSkillItem->set_skilllevel(pConfig->vecArenaRobotSkillCfg[i].Lv);
		}
	}
	
	//tmpInfo.basedata().set_logouttime(Time::Now().UnixSec());  //设置这个，是为了在背包部件初始化的时候，不在给初始装备
	//初始化玩家部件初始化
	m_pPart[PART_MOVE] = g_GetCreatureMgr()->CreatePart(PART_MOVE, dynamic_cast<Creature*>(this), &tmpInfo);
	if (!m_pPart[PART_MOVE])
		return false;
	m_pPart[PART_SKILL] = g_GetCreatureMgr()->CreatePart(PART_SKILL, dynamic_cast<Creature*>(this), &tmpInfo);
	if (!m_pPart[PART_SKILL])
		return false;
	m_pPart[PART_BUFF] = g_GetCreatureMgr()->CreatePart(PART_BUFF, dynamic_cast<Creature*>(this), &tmpInfo);
	if (!m_pPart[PART_BUFF])
		return false;
	m_pPart[PART_PACKAGE] = g_GetCreatureMgr()->CreatePart(PART_PACKAGE, dynamic_cast<Creature*>(this),NULL);
	if (!m_pPart[PART_PACKAGE])
		return false;
	PackagePart * pPart = dynamic_cast<PackagePart*>(m_pPart[PART_PACKAGE]);
	if (pPart)
	{
		for (int i = 0; i < (int)pConfig->vec_equip.size();++i)
		{
			pPart->AddEquipOnBody(pConfig->vec_equip[i]);
		}
		pPart->UpdateFacade();
	}

	FinallyCalcChangeAttr();

	if (InitAI() == false)
	{
		return false;
	}

	return true;
}


bool CRebot::InitAI()
{
	m_startAiTime = g_GetGlobalServerTime()->Tick() + 10000; //after 10s, start ai
	m_aiData = g_GetBehaviacAIMgr()->CreateAI("RobotCommonAI", m_Cid);
	if (m_aiData == nullptr)
	{
		return false;
	}
	m_aiData->Init(this);
	if (!g_GetTimerAxis()->SetTimer(REBOT_START_AI, 10000, dynamic_cast<ITimerHandler*>(this), 1))
	{
		LogErrFmtPrint("CreatureCommonAI settimer fail, timeId:%d, cid:%d", AI_TIME_COMBAT, GetCid());
	}
	return true;
}


bool CRebot::Init(CharacterDBInfo &PlayerInfo, ENTER_ACCOUNT* account)
{
	memset(m_attrs, 0, sizeof(uint32_t)*P_MAX);
	for (uint32_t i = 0; i < PART_MAX; i++)
	{
		m_pPart[i] = nullptr;
	}

	ReadBaseData(PlayerInfo);
	//SetAccountInfo(account);


	//初始化玩家部件初始化
	m_pPart[PART_MOVE] = g_GetCreatureMgr()->CreatePart(PART_MOVE, dynamic_cast<Creature*>(this), &PlayerInfo);
	if (!m_pPart[PART_MOVE])
		return false;
	m_pPart[PART_SKILL] = g_GetCreatureMgr()->CreatePart(PART_SKILL, dynamic_cast<Creature*>(this), &PlayerInfo);
	if (!m_pPart[PART_SKILL])
		return false;
	m_pPart[PART_BUFF] = g_GetCreatureMgr()->CreatePart(PART_BUFF, dynamic_cast<Creature*>(this), &PlayerInfo);
	if (!m_pPart[PART_BUFF])
		return false;
	m_pPart[PART_PACKAGE] = g_GetCreatureMgr()->CreatePart(PART_PACKAGE, dynamic_cast<Creature*>(this), &PlayerInfo);
	if (!m_pPart[PART_PACKAGE])
		return false;

	//初始化角色等级相关属性
	InitLevAttrs();

	SetAttrCache(g_GetAttrMgr()->GetPlayerFightModlePAttr(enFightAttrModule_Level, C_MSPEED), 5.2 * 1000);

	m_isFightState = false;
	m_startFightStateTime = 0;


	FinallyCalcChangeAttr();

	if (InitAI() == false)
	{
		return false;
	}

	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_UNDER_ATTACK, m_Cid, 0, NULL);
	g_GetEvent()->Subscribe(dynamic_cast<IEventExecuteSink*>(this), EVENT_ATTACK_SOMEBODY, m_Cid, 0, NULL);
	return true;
}

bool CRebot::UnInit()
{

	//memset(m_attrs, 0, sizeof(uint32_t)*M_MAX);

	//for (int32_t i = 0; i < PART_MAX; i++)
	//{
	//	if (m_pPart[i])
	//	{
	//		g_GetPlayerMgr()->RecylePart(m_pPart[i]);
	//		m_pPart[i] = nullptr;
	//	}
	//}

	//LeaveScene();
	Player::UnInit();

	return true;
}

void CRebot::Update(uint64_t tick)
{
	//uint64_t curTick = g_GetGlobalServerTime()->Tick();
	//Creature::Update(tick);

	////部件update
	//for (int32_t i = 0; i < PART_MAX; i++)
	//{
	//	if (m_pPart[i])
	//		m_pPart[i]->Update(tick);
	//}

	//if (g_GetGlobalServerTime()->Tick() > m_startAiTime)
	//{
	//	UpdateAiInfo(tick);
	//}
}

bool CRebot::EnterScene(uint32_t sceneId, Point3<float>& enterPos, STransParam &transParam)
{
	Player::EnterScene(sceneId, enterPos, transParam);
	//UpdateNineGridLst();
	return true;
}

bool CRebot::LeaveScene()
{
	Player::LeaveScene();
	return true;
}

void CRebot::OnTimer(uint32_t nTimerID)
{
	Player::OnTimer(nTimerID);
	switch (nTimerID)
	{
	case REBOT_START_AI:
	{
		if (m_aiData)
		{
			if (!g_GetTimerAxis()->SetTimer(AI_TIME_COMBAT, 200, dynamic_cast<ITimerHandler*>(m_aiData), INFINITY_CALL))
			{
				LogErrFmtPrint("CreatureCommonAI settimer fail, timeId:%d, cid:%d", AI_TIME_COMBAT, GetCid());
				return;
			}
		}
	}
		break;
	default:
		break;
	}
}

void CRebot::OnDead(uint64_t killerCid)
{
	//Creature::OnDead(killerCid);
	m_curstate = State_dead;
	SynAttrToClient();

	DieEvent dieEvent;
	dieEvent.killerCid = killerCid;
	dieEvent.count = 1;
	dieEvent.sceneId = m_sceneId;
	dieEvent.nCid = m_Cid;
	g_GetEvent()->FireExecute(EVENT_CREATURE_DEAD, killerCid, m_creatureKind, (DieEvent*)&dieEvent, sizeof(dieEvent));

	LogDebugFmtPrint("robot ondead, cid:%d", m_Cid);
}

//void CRebot::GetVisibleDataToClient(CreaturesCreateData& cvData)
//{
	//Player::GetVisibleDataToClient(cvData);
	//CreaturesCreateData::Player *pPlayerVinfo = cvData.add_players();
	//if (nullptr == pPlayerVinfo)
	//{
	//	return;
	//}
	//pPlayerVinfo->set_playerid(m_Cid);
	//pPlayerVinfo->set_level(m_attrs[P_LEVEL]);
	//pPlayerVinfo->set_playername(m_name);
	//pPlayerVinfo->set_hp(m_attrs[P_HP]);
	//pPlayerVinfo->set_maxhp(m_attrs[P_MAX_HP]);
	//pPlayerVinfo->set_posx(m_pos.x);
	//pPlayerVinfo->set_posy(m_pos.y);
	//pPlayerVinfo->set_posz(m_pos.z);
	//pPlayerVinfo->set_prof(m_attrs[P_PROF]);
	//pPlayerVinfo->set_gender(m_attrs[P_GENDER]);
	//pPlayerVinfo->set_playerrace(m_attrs[P_RACE]);
	//pPlayerVinfo->set_namebody(0);
	//pPlayerVinfo->set_nameweapon(0);
	//pPlayerVinfo->set_curstate(m_curstate);
	//pPlayerVinfo->set_mspeed(m_attrs[P_MSPEED]*1.0/1000);

	//pPlayerVinfo->set_team_id(m_attrs[P_TEAM_ID]);
	//pPlayerVinfo->set_is_leader(m_attrs[P_TEAM_LEADER_ID]);

	//uint32_t weaponId = 0;
	//uint32_t clothesId = 0;
	//uint32_t wingId = 0;
	//GetFacade(clothesId, weaponId, wingId);
	//pPlayerVinfo->set_namebody(clothesId);
	//pPlayerVinfo->set_nameweapon(weaponId);



//	CreaturesCreateData::Rebot *pRebotData = cvData.add_rebots();
//	pRebotData->set_cid(m_Cid);
//	pRebotData->set_configid(m_nRebotID);
//	pRebotData->set_hp(m_attrs[M_HP]);
//	pRebotData->set_maxhp(m_attrs[M_MAX_HP]);
//	pRebotData->set_posx(m_pos.x);
//	pRebotData->set_posy(m_pos.y);
//	pRebotData->set_posz(m_pos.z);
//	pRebotData->set_curstate(m_curstate);
//	pRebotData->set_mspeed(m_fspeed);
//	uint32_t weaponId = 0;
//	uint32_t clothesId = 0;
//	uint32_t wingId = 0;
//	GetFacade(clothesId, weaponId, wingId);
//	pRebotData->set_namebody(clothesId);
//	pRebotData->set_nameweapon(weaponId);
//}
